package com.avic.system.controller;

import com.avic.system.common.constant.UserConstant;
import com.avic.system.common.convertor.SystemLogConvertor;
import com.avic.system.common.util.AssertUtil;
import com.avic.system.common.util.ex.BizErrorCode;
import com.avic.system.common.util.lang.ServiceCallBack;
import com.avic.system.controller.request.GetSystemLogListReq;
import com.avic.system.controller.result.GetSystemLogListRes;
import com.avic.system.dal.condition.FindSystemLogListCondition;
import com.avic.system.dal.model.SystemLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@Slf4j
@RestController
@RequestMapping("/system/log")
@Api(tags = "系统操作日志", description = "系统日志列表")
public class SystemLogController extends AbstractFacadeImpl {

    @ApiOperation(value = "系统操作日志列表管理", notes = "系统操作列表管理")
    @PostMapping("list")
    public GetSystemLogListRes getSystemLogList(@RequestBody GetSystemLogListReq getSystemLogListReq,
                                                @RequestHeader(value = UserConstant.HEADER_CURRENT_USER_ID_KEY, required = false) String currentUserId) {
        log.info("系统日志列表管理--getLoginLogList");
        final GetSystemLogListRes result = new GetSystemLogListRes();
        return serviceExecuteTemplate.executeService(result, new ServiceCallBack() {
            @Override
            public void check() throws Exception {
                AssertUtil.isTrue(getSystemLogListReq.getCurrentPage() > 0,
                        BizErrorCode.PARAM_ERROR, "查看页数必须大于零");
            }

            @Override
            public void doService() throws Exception {
                FindSystemLogListCondition findSystemLogListCondition =
                        new FindSystemLogListCondition(getSystemLogListReq.getCurrentPage(), getSystemLogListReq.getPageSize());
                findSystemLogListCondition.setLogBeginTime(getSystemLogListReq.getLogBeginTime());
                findSystemLogListCondition.setLogEndTime(getSystemLogListReq.getLogEndTime());
                findSystemLogListCondition.setUserName(getSystemLogListReq.getUserName());

                if (!StringUtils.equals(currentUserId, "1")) {
                    findSystemLogListCondition.setUserId(currentUserId);
                }

                List<SystemLog> systemLogs = systemLogService.systemLogList(findSystemLogListCondition);
                result.setDataList(SystemLogConvertor.systemLogsToSystemLogInfoList(systemLogs));
                result.setTotalRecord(systemLogService.logListTotalCount(findSystemLogListCondition));
                result.setSuccess(true);
            }
        });
    }
}
